import { toRefs, reactive, computed, onUnmounted, onMounted, ref } from 'vue';
import { getBrowerInfo, MD5, uuid } from '@/common/utils/util';
import * as util from '@/tool/function/util';
import systemConfig from '@/common/config';
import { onLoad } from '@dcloudio/uni-app';
export default function () {
  const messageRef = ref();
  const state = reactive({
    loginCnt: 0,
    changeSuccess: false,
    ruleForm: {
      cellphone: '',
      shortMsgCode: '',
      userType: '20',
      imgCode: '',
      userPassword: '',
    },
    showImgCode: false,
    disableCodeBtn: false,
    msgForm: {
      time: 60,
      loginBtn: false,
      smsSendBtn: false,
      loginType: 6,
      smsText: '获取验证码',
    },
    imgSrc: '',
    loading: {
      signIn: false,
    },
    category: 'EASY_SPEC',
  });

  let loginKey = ref(uuid());
  let browser = getBrowerInfo();
  let interval: any = null;
  const getCode = () => {
    let params = { category: state.category };
    $api.common.send
      .postCrypto('login0004', params)
      .then((res: any) => {
        let result = res.result;
        loginKey.value = result.loginKey;
        state.imgSrc = result.graphicImageBase64;
      })
      .catch(() => {});
  };
  onMounted(() => {
    getCode();
  });
  onLoad((option: any) => {
    if (option.userType) {
      state.ruleForm.userType = option.userType;
    }
  });
  const sendMessage = () => {
    if (state.disableCodeBtn) {
      return;
    }
    if (!state.ruleForm.cellphone) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '请输入手机号码！';
      messageRef.value.show(messageData);
      return;
    } else {
      if (!util.isPhone(state.ruleForm.cellphone)) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请输入正确的手机号码！';
        messageRef.value.show(messageData);
        return;
      }
    }
    state.disableCodeBtn = true;
    interval = window.setInterval(() => {
      if (state.msgForm.time-- <= 0) {
        state.msgForm.time = 60;
        state.disableCodeBtn = false;
        state.msgForm.smsSendBtn = false;
        state.msgForm.smsText = '重新发送';
        window.clearInterval(interval);
        //getCode()
      }
    }, 1000);
    let params = {
      loginKey: loginKey.value,
      category: state.category,
      smsSendType: 'forgetPassword',
    };
    params = Object.assign(params, state.ruleForm);
    $api.common.send
      .postCrypto('login0005', params,null,true)
      .then(() => {
        state.disableCodeBtn = true;
        state.msgForm.smsSendBtn = true;
        let messageData: any = {};
        messageData.model = 'success';
        messageData.text = '发送成功！';
        messageRef.value.show(messageData);
      })
      .catch(() => {
        clearInterval(interval);
        state.msgForm.time = 60;
        state.disableCodeBtn = false;
        state.msgForm.smsSendBtn = false;
        state.msgForm.smsText = '获取验证码';
      });
  };
  const imgChange = (value: any) => {
    if (state.msgForm.smsSendBtn) {
      state.disableCodeBtn = true;
      return;
    }
    if (value && value.length < 4) {
      state.disableCodeBtn = true;
    } else {
      state.disableCodeBtn = false;
    }
  };
  const imgClear = () => {
    state.disableCodeBtn = true;
  };
  // 登录
  const signIn = async () => {
    if (!state.ruleForm.cellphone) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '请输入手机号码！';
      messageRef.value.show(messageData);
      return;
    } else {
      if (!util.isPhone(state.ruleForm.cellphone)) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请输入正确的手机号码！';
        messageRef.value.show(messageData);
        return;
      }
    }
    if (!state.ruleForm.shortMsgCode) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '请输入手机验证码！';
      messageRef.value.show(messageData);
      return;
    }
    if (!state.ruleForm.userPassword) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '请输入新密码！';
      messageRef.value.show(messageData);
      return;
    }
    if (state.showImgCode && !state.ruleForm.imgCode) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '请输入图片验证码！';
      messageRef.value.show(messageData);
      return;
    }
    state.loading.signIn = true;
    const data = {};
    data.cellphone = state.ruleForm.cellphone;
    data.shortMsgCode = state.ruleForm.shortMsgCode;
    data.userPassword = md5(state.ruleForm.userPassword);
    data.userType = state.ruleForm.userType;
    data.imgCode = state.ruleForm.imgCode;
    let res = await $api.common.send
      .postCrypto('login0014', data)
      .then((res) => {
        state.loading.signIn = true;
        state.changeSuccess = true;
        setTimeout(() => gotoLogin(), 2000);
      })
      .catch((error) => {
        state.loading.signIn = false;
        state.loginCnt = state.loginCnt + 1;
        if (state.loginCnt >= 5) {
          state.showImgCode = true;
          if (!state.imgSrc) {
            getCode();
          }
        }
      });
  };
  const gotoLogin = () => {
    let page = getCurrentPages();
    if (page) {
      if (page.length <= 1) {
        $router.replaceAll('/pages/system/login/login1/index');
      } else {
        $router.back(1);
      }
    } else {
      $router.replaceAll('/pages/main/login/login1/index');
    }
  };
  onUnmounted(() => {
    if (interval) {
      window.clearInterval(interval);
    }
  });
  return {
    sendMessage,
    signIn,
    imgClear,
    imgChange,
    getCode,
    messageRef,
    gotoLogin,
    ...toRefs(state),
  };
}
